package com.jinzhu.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jinzhu.dao.ClazzMapper;
import com.jinzhu.pojo.Clazz;

import com.jinzhu.pojo.PageBean;

import com.jinzhu.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    // 根据内容分页查询班级
    @Override
    public PageBean AllClass1(String name, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Clazz> clazzes = clazzMapper.AllClass1(name, begin, end);
        Page<Clazz> empPage = (Page<Clazz>) clazzes;
        PageBean pageBean = new PageBean(empPage.getTotal(), empPage.getResult());
        return pageBean;

    }


    // 查询所有班级
    @Override
    public List<Clazz> AllClass() {
        List<Clazz> clazzes = clazzMapper.AllClass();
        return clazzes;
    }

    // 根据ID删除班级
    @Override
    public Integer delete(Integer id) {
        return clazzMapper.delete(id);

    }

    // 添加班级
    @Override
    public Integer add(Clazz clazz) {
        if (StringUtils.hasLength(clazz.getName()) || StringUtils.hasLength(clazz.getRoom()) || clazz.getBeginDate() != null || clazz.getEndDate() != null || clazz.getSubject() != null) {
            clazz.setCreateTime(LocalDateTime.now());
            return clazzMapper.add(clazz);
        }
        return 0;


    }

    // 根据ID查询班级
    @Override
    public Clazz get(Integer id) {
        Clazz clazz = clazzMapper.get(id);
        return clazz;
    }

    // 修改班级
    @Override
    public Integer update(Clazz clazz) {
        if (clazz.getId() != null || StringUtils.hasLength(clazz.getName()) || StringUtils.hasLength(clazz.getRoom()) || clazz.getBeginDate() != null || clazz.getEndDate() != null || clazz.getMasterId() != null) {
            clazz.setUpdateTime(LocalDateTime.now());
            return clazzMapper.update(clazz);
        }
        return 0;

    }
}








